import {Component, OnInit} from '@angular/core';
import {Globals} from '../../../../globals';
import {ApiUrl} from '../../../../config/api-url';
import {BaseService} from '../../../../shared/service/base.service';
import {PageHelper} from '../../../../utils/page-helper-model';
import {NoticeTypeEnum} from '../../../../enums/notice-type.enum';
import {NoticeStatusEnum} from '../../../../enums/notice-status.enum';

@Component({
  selector: 'app-notice-list',
  templateUrl: './notice-list.component.html',
  styleUrls: ['./notice-list.component.less']
})
export class NoticeListComponent implements OnInit {
  // 查看运行当前设备
  isMobile = Globals.isMobile();
  // 数据集合
  listOfData: any;
  // 显示模态框
  isVisible = false;
  // 加载动画
  isOkLoading: boolean;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  // 当前对象，用于传值给子组件
  notice: any;
  // 公告类型枚举
  noticeTypeEnum = NoticeTypeEnum;
  // 公告状态枚举
  noticeStatusEnum = NoticeStatusEnum;

  constructor(public baseService: BaseService) {
  }

  ngOnInit(): void {
    this.findList();
  }

  // 获取列表
  findList() {
    this.baseService.findListByKeyword(ApiUrl.NoticeUrl + '/list', this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.listOfData = data.data.list;
          this.pageHelper.totalItems = data.data.total;
          this.pageHelper.numPages = data.data.pages;
        }
      }
    );
  }

  // 根据keyword条件获取列表
  findListByKeyword() {
    this.findList();
  }

  // 子组件向父组件传值
  setVisible(body) {
    this.isVisible = body.isVisible;
  }

  // 显示模态框
  addNotice() {
    this.isVisible = true;
  }

  // 修改
  updateNotice(noticeItem) {
    this.notice = noticeItem;
    this.isVisible = true;
  }

  // 将状态改为失效
  stopNotice(id: any) {

    this.isOkLoading = true;
    let body = {
      id: id,
      status: this.noticeStatusEnum.FAILURE
    };

    this.baseService.put(ApiUrl.NoticeUrl, body).subscribe(
      res => {
        if (res.status == 0) {
          this.baseService.layer.messageSuccess('修改成功！');
          this.loadData();
        } else {
          this.baseService.layer.notificationError('修改失败' + res.msg);
        }
      }
    );
  }

  loadData() {
    this.pageHelper.currentPage = 1;
    this.findList();
  }

  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.findList();
  }
}
